﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using MVCDemo.Core.Cache;
namespace MVCDemo.Web.Controllers
{
    public class CacheController : Controller
    {
        IMemoryCache _cache = null;
        ICacheService _redisCache = null;
        public CacheController(IMemoryCache memoryCache,ICacheService redisCache)
        {
            _cache = memoryCache;
            _redisCache = redisCache;
        }
        public IActionResult Index()
        {
            //MemoryCache示例
            string memoryCache = _cache.Get<string>("memoryCacheTest");
            if (string.IsNullOrEmpty(memoryCache))
            {
                //添加并且设置缓存
                _cache.Set<string>("memoryCacheTest", "我是memoryCacheTest缓存");
                memoryCache = _cache.Get<string>("memoryCacheTest");
            }
            ViewData["memoryCache"] = memoryCache;
            //RedisCache示例
            string redisCache = _redisCache.Get("redisCacheTest");
            if (string.IsNullOrEmpty(redisCache))
            {
                //添加并且设置缓存
                _redisCache.Add("redisCacheTest", "我是redisCacheTest缓存");
                redisCache = _redisCache.Get("redisCacheTest");
            }
            ViewData["redisCache"] = redisCache;
            return View();
        }
    }
}